package com.mall.b2c.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mall.common.util.CookieUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mall.b2c.bean.IntergralShopClassBean;
import com.mall.b2c.bean.IntergralShopOrderBean;
import com.mall.b2c.bean.IntergralShopProductBean;
import com.mall.b2c.bean.MemberBean;
import com.mall.b2c.hessian.ContentHessian;
import com.mall.b2c.hessian.IntergralHessian;
import com.mall.common.bean.Message;
import com.mall.common.exception.NoticeException;
import com.mall.common.util.AppContext;
import com.mall.common.validation.annotation.RequiredStringValidator;
import com.mall.common.validation.annotation.Validations;

import static com.mall.b2c.Constants.CART_COOKIE_QUANTITY;

@Controller
@RequestMapping("/intergral")
public class IntergralController extends BaseController{
	@Resource private IntergralHessian intergralHessian;
	@RequestMapping("/index")
	public String index(Model model,@RequestParam(required=false)Long classId){
		model.addAttribute("productList",jsonToList(intergralHessian.getShopProductList(classId,"id asc"), IntergralShopProductBean.class));
		model.addAttribute("intergral", intergralHessian.getIntergralByUserId(getMemberId()));
		model.addAttribute("classId", classId);
		return "/intergral/index";
	}
	@RequestMapping("/content/{id}")
	public String detail(Model model,@PathVariable Long id){
		model.addAttribute("intergral", intergralHessian.getIntergralByUserId(getMemberId()));
		model.addAttribute("detail", jsonToObj(intergralHessian.getProductDetail(id), IntergralShopProductBean.class));
		return "/intergral/content";
	}
	@Validations(
			requiredStrings = {
					@RequiredStringValidator(fieldName="name",message="姓名不能为空"),
					@RequiredStringValidator(fieldName="phone",message="手机号不能为空"),
					@RequiredStringValidator(fieldName="address",message="地址不能为空"),
					@RequiredStringValidator(fieldName="num",message="数量不能为空")
			}
	)
	
	 @RequestMapping(value="/submit", method=RequestMethod.POST)
	@ResponseBody
	public JSONObject detail(Model model,@RequestParam(required=false)String name,@RequestParam(required=false)String phone,@RequestParam(required=false)String address,@RequestParam(required=false)int num,@RequestParam(required=false)Long productId){
		//orderBean.setMemberId(getMemberId());
		JSONObject jObj = new JSONObject();
		try {
			String message = intergralHessian.exchange(name,phone,address,num,productId,getMemberId());
			if(message.equals("0")){
				jObj.put("error","兑换失败");
			}else{
				jObj.put("success","兑换成功");
			}
			
		} catch (NoticeException e) {
			jObj.put("error", e.getMessage());
			//jObj.put("showCode", "");
		}
		return jObj;
		
	}

	@Validations(
			requiredStrings = {
					@RequiredStringValidator(fieldName="name",message="姓名不能为空"),
					@RequiredStringValidator(fieldName="phone",message="手机号不能为空"),
					@RequiredStringValidator(fieldName="address",message="地址不能为空"),
			}
	)
	@RequestMapping(value="/create", method=RequestMethod.POST)
	@ResponseBody
	public Message submit(String name,String phone,String address, String token, HttpServletRequest request,Long store, HttpServletResponse response){

		Object obj = AppContext.getSessionAttr(token);
		if(store==null){
			throw new NoticeException("请选择配送门店");
		}
		if(obj == null){
			throw new NoticeException("当前页面已过期");
		}
		if(obj instanceof String){
			throw new NoticeException("购买信息有误");
		}else if(obj instanceof Long[]){
			Long[] cartItemIds = (Long[]) obj;
			Long storeId = getCurrStoreId();
			intergralHessian.createOrder(getCurrStoreId(), getMemberId(), cartItemIds, name, phone, address,store);
			String numStr = CookieUtils.get(request, CART_COOKIE_QUANTITY);
			if(StringUtils.isNotBlank(numStr)){
				int num = Integer.valueOf(numStr);
				num = num - cartItemIds.length;
				CookieUtils.addGlobalCookie(request, response, CART_COOKIE_QUANTITY, String.valueOf(num>0 ? num : 0), null);
			}
		}else{
			throw new NoticeException("数据有误");
		}

		return successMessage;

	}
	
}
